package o;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class aar<T, Y> {
    private final long ˊ;
    private long ˋ;
    private long ˎ;
    private final Map<T, Y> ˏ = new LinkedHashMap(100, 0.75f, true);

    public aar(long j) {
        this.ˊ = j;
        this.ˎ = j;
    }

    protected int ॱ(@Nullable Y y) {
        return 1;
    }

    protected void ॱ(@NonNull T t, @Nullable Y y) {
    }

    public synchronized long ˊ() {
        return this.ˎ;
    }

    @Nullable
    public synchronized Y ˏ(@NonNull T t) {
        return this.ˏ.get(t);
    }

    @Nullable
    public synchronized Y ˊ(@NonNull T t, @Nullable Y y) {
        Y y2;
        int ॱ = ॱ(y);
        if (((long) ॱ) >= this.ˎ) {
            ॱ(t, y);
            y2 = null;
        } else {
            if (y != null) {
                this.ˋ = ((long) ॱ) + this.ˋ;
            }
            y2 = this.ˏ.put(t, y);
            if (y2 != null) {
                this.ˋ -= (long) ॱ(y2);
                if (!y2.equals(y)) {
                    ॱ(t, y2);
                }
            }
            ˏ();
        }
        return y2;
    }

    @Nullable
    public synchronized Y ˋ(@NonNull T t) {
        Y remove;
        remove = this.ˏ.remove(t);
        if (remove != null) {
            this.ˋ -= (long) ॱ(remove);
        }
        return remove;
    }

    public void ˋ() {
        ˎ(0);
    }

    protected synchronized void ˎ(long j) {
        while (this.ˋ > j) {
            Iterator it = this.ˏ.entrySet().iterator();
            Entry entry = (Entry) it.next();
            Object value = entry.getValue();
            this.ˋ -= (long) ॱ(value);
            Object key = entry.getKey();
            it.remove();
            ॱ(key, value);
        }
    }

    private void ˏ() {
        ˎ(this.ˎ);
    }
}
